void processRestock() {
    CLEAR_SCREEN;
    printf("==================== 电商平台综合管理系统 - 处理补货 ====================\n\n");

    printf("低库存产品:\n");
    printf("------------------------------------\n");

    int lowStockCount = 0;
    for (int i = 0; i < productCount; i++) {
        if (products[i].stock < RESTOCK_THRESHOLD) {
            lowStockCount++;
            printf("产品: %s, 当前库存: %d, 需补货: %d\n",
                   products[i].name,
                   products[i].stock,
                   RESTOCK_THRESHOLD - products[i].stock + 50);
        }
    }

    if (lowStockCount == 0) {
        printf("没有需要补货的产品，所有产品库存充足。\n");
    } else {
        printf("\n正在进行补货操作，为每个低库存产品补充50个单位。\n");
        for (int i = 0; i < productCount; i++) {
            if (products[i].stock < RESTOCK_THRESHOLD) {
                products[i].stock += 50;
            }
        }
    }

    printf("------------------------------------\n\n");
    pauseExecution();
    showInventoryManagement();
}

// 显示销售统计
void showSalesReports() {
    CLEAR_SCREEN;
    printf("==================== 电商平台综合管理系统 - 销售统计 ====================\n\n");

    printf("销售数据概览:\n");
    printf("------------------------------------\n");
    printf("总销售额: ￥%.2f (同比增长 15%%)\n",
           products[0].price * products[0].sales +
           products[1].price * products[1].sales);
    printf("订单数量: %d (同比增长 10%%)\n", orderCount);
    printf("总产品销量: %d (同比增长 20%%)\n", products[0].sales + products[1].sales + products[2].sales);
    printf("客单价: ￥%.2f (环比增长 5%%)\n",
           (products[0].price * products[0].sales + products[1].price * products[1].sales) /
           (orderCount > 0 ? orderCount : 1));
    printf("------------------------------------\n\n");

    printf("1. 查看销售趋势\n");
    printf("2. 查看畅销产品\n");
    printf("0. 返回上一级\n\n");
    printf("请选择: ");

    int choice = getIntInput();
    switch (choice) {
        case 1: viewSalesTrend(); break;
        case 2: viewBestSellers(); break;
        case 0: showDashboard(); break;
        default: printf("无效选择。"); pauseExecution(); showSalesReports();
    }
}

// 查看销售趋势
void viewSalesTrend() {
    CLEAR_SCREEN;
    printf("==================== 电商平台综合管理系统 - 销售趋势 ====================\n\n");

    printf("产品销售情况 (近30天):\n");
    printf("------------------------------------\n");
    printf("产品\t\t销量\n");
    printf("------------------------------------\n");
    printf("%s\t\t%d\n", products[0].name, products[0].sales);
    printf("%s\t\t%d\n", products[1].name, products[1].sales);
    printf("%s\t\t%d\n", products[2].name, products[2].sales);
    printf("------------------------------------\n\n");

    printf("销售占比:\n");
    printf("------------------------------------\n");
    printf("数码产品: %.2f%%\n",
           (float)(products[0].sales + products[1].sales) /
           (products[0].sales + products[1].sales + products[2].sales) * 100);
    printf("服装: %.2f%%\n",
           (float)products[2].sales /
           (products[0].sales + products[1].sales + products[2].sales) * 100);
    printf("------------------------------------\n\n");

    pauseExecution();
    showSalesReports();
}

// 查看畅销产品
void viewBestSellers() {
    CLEAR_SCREEN;
    printf("==================== 电商平台综合管理系统 - 畅销产品 ====================\n\n");

    printf("畅销产品排行:\n");
    printf("------------------------------------\n");
    printf("排名\t产品\t\t类别\t\t销量\t销售额\n");
    printf("------------------------------------\n");

    // 复制产品数组
    Product temp;
    Product sortedProducts[MAX_PRODUCTS];
    memcpy(sortedProducts, products, productCount * sizeof(Product));

    // 冒泡排序
    for (int i = 0; i < productCount - 1; i++) {
        for (int j = 0; j < productCount - i - 1; j++) {
            if (sortedProducts[j].sales < sortedProducts[j + 1].sales) {
                temp = sortedProducts[j];
                sortedProducts[j] = sortedProducts[j + 1];
                sortedProducts[j + 1] = temp;
            }
        }
    }

    for (int i = 0; i < productCount; i++) {
        printf("%d\t%-15s\t%-10s\t%d\t￥%.2f\n",
               i + 1,
               sortedProducts[i].name,
               sortedProducts[i].category,
               sortedProducts[i].sales,
               sortedProducts[i].price * sortedProducts[i].sales);
    }
    printf("------------------------------------\n\n");

    pauseExecution();
    showSalesReports();
}

// 显示主菜单
void showMainMenu() {
    CLEAR_SCREEN;
    printf("==================== 电商平台综合管理系统 ====================\n\n");
    printf("1. 查看仪表盘\n");
    printf("2. 用户管理\n");
    printf("3. 产品管理\n");
    printf("4. 订单管理\n");
    printf("5. 库存管理\n");
    printf("6. 销售统计\n");
    printf("0. 退出系统\n\n");
    printf("请选择: ");

    int choice = getIntInput();
    switch (choice) {
        case 1: showDashboard(); break;
        case 2: showUserManagement(); break;
        case 3: showProductManagement(); break;
        case 4: showOrderManagement(); break;
        case 5: showInventoryManagement(); break;
        case 6: showSalesReports(); break;
        case 0: printf("感谢使用，再见！\n"); exit(0);
        default: printf("无效选择。"); pauseExecution(); showMainMenu();
    }
}

// 清理旧订单
void cleanupOldOrders() {
    clock_t now = clock();
    int newOrderCount = 0;
    for (int i = 0; i < orderCount; i++) {
        if (orders[i].status == COMPLETED && (now - orders[i].orderTime) > ORDER_CLEANUP_THRESHOLD) {
            // 跳过需要删除的订单
            continue;
        }
        // 保留不需要删除的订单
        orders[newOrderCount] = orders[i];
        newOrderCount++;
    }
    orderCount = newOrderCount;
    printf("清理旧订单完成，当前订单数量: %d\n", orderCount);
}

int main() {
    initData();
    // 清理旧订单
    cleanupOldOrders();
    showMainMenu();
    return 0;
}